package com.tianji.common.utils;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.Serializable;
import java.time.LocalDateTime;

/***
 * 获取当前登录用户信息，使用解析jwt中的用户信息
 */
@Slf4j
public class SecurityUtil {

    public static XcUser getUser() {
        try {
            Object principalObj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (principalObj instanceof String) {
                //取出用户身份信息
                String principal = principalObj.toString();
                //将json转成对象
                XcUser user = JSON.parseObject(principal, XcUser.class);
                return user;
            }
        } catch (Exception e) {
            log.error("获取当前登录用户身份出错:{}", e.getMessage());
            e.printStackTrace();
        }

        return null;
    }

    @Data
    public static class XcUser implements Serializable {

        private static final long serialVersionUID = 1L;

        @ApiModelProperty(value = "ID")
        @TableId(value = "user_id", type = IdType.AUTO)
        private Long userId;

        @ApiModelProperty(value = "部门名称")
        private Long deptId;

        @ApiModelProperty(value = "用户名")
        private String username;

        @ApiModelProperty(value = "昵称")
        private String nickName;

        @ApiModelProperty(value = "性别")
        private String gender;

        @ApiModelProperty(value = "手机号码")
        private String phone;

        @ApiModelProperty(value = "邮箱")
        private String email;

        @ApiModelProperty(value = "头像地址")
        private String avatarName;

        @ApiModelProperty(value = "头像真实路径")
        private String avatarPath;

        @ApiModelProperty(value = "密码")
        private String password;

        @ApiModelProperty(value = "是否为admin账号")
        private Boolean isAdmin;

        @ApiModelProperty(value = "状态：1启用、0禁用")
        private Boolean enabled;

        @ApiModelProperty(value = "创建者")
        private String createBy;

        @ApiModelProperty(value = "更新者")
        private String updateBy;

        @ApiModelProperty(value = "修改密码的时间")
        private LocalDateTime pwdResetTime;

        @ApiModelProperty(value = "创建日期")
        private LocalDateTime createTime;

        @ApiModelProperty(value = "更新时间")
        private LocalDateTime updateTime;


    }


}
